import router, { constantRoutes } from '@/router'
import api from '@/api/user'
import { routeMap } from '@/const'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
//   console.log(roles)
//   let arr = [
//     {
//       code:'PERSONAL',
//       level:"1"
//     },
//     {
//       code:'PERSONAL_C'
//     }
//   ]
//   let check = true
//   for(let i = 0; i < arr.length; i ++ ){
//     roles.map(item=>{
//       if(item.code == arr[i].code){
//         check = false
//       }
//     })
//   }
//   if(check){
//     roles= [roles,...arr]
//   }
  return roles.some(role => routeMap[role.code] === route.path)
}
function insectPermission(roles){
  if(roles.some(role => role.code === 'GIS')||roles.some(role => role.code === 'GIS_AROUND')){
    roles.push({code:'GIS_SYS'})
  }
  return roles
}
/**
 * Filter asynchronous routing tables by recursion
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }

    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }else if(tmp.path==='/'||tmp.path==='/redirect'||tmp.path==='/login'||tmp.path==='/dashboard'||tmp.path==='/helpcenter'){
      res.push(tmp)
    }
  })

  return res
}

const menulist = []
const permissionlist = []
const rolelist = []
const state = {
  menulist: menulist,
  permissionlist: permissionlist,
  rolelist:rolelist,
}

const mutations = {
  SET_MENU: (state, menus) => {
    state.menulist = menus
    localStorage.setItem('menulist', JSON.stringify(menus))
  },
  DEL_MENU: state => {
    state.menulist = []
    localStorage.removeItem('menulist')
  },
  SET_PERMISSION: (state, permissions) => {
    state.permissionlist = permissions
    localStorage.setItem('permissionlist', permissions?JSON.stringify(permissions):'')
  },
  DEL_PERMISSION: state => {
    state.permissionlist = []
    localStorage.removeItem('permissionlist')
  },
  SET_ROLE_PERMISSION: (state, rolelist) => {
    state.rolelist = rolelist
    localStorage.setItem('rolelist', rolelist?JSON.stringify(rolelist):'')
  },
  DEL__ROLE_PERMISSION: state => {
    state.rolelist = []
    localStorage.removeItem('rolelist')
  },
}

const actions = {
  delMenuList({ commit }) {
    commit('DEL_MENU')
  },
  generateRoutes({ commit }) {
    return new Promise(resolve => {
      // commit('SET_MENU', constantRoutes)
      // router.addRoutes(constantRoutes)
      // resolve(constantRoutes)
      api.permission().then(res => {
        console.log(res,'kkkk=========')
          let roles = res.functionAndModePermission
          let arr = [
            {
              code:'PERSONAL',
              level:"1"
            },
            {
              code:'PERSONAL_C'
            }
          ]
          roles = [...roles,...arr]
        console.log(roles,'kkkk=========')

          let permissions = res.methodsPermission
          commit('SET_PERMISSION', permissions)
          commit('SET_ROLE_PERMISSION', roles)
          if(roles){
            // insectPermission(roles)
            let accessedRoutes = filterAsyncRoutes(constantRoutes, roles)
            // console.log('permissions ----------', accessedRoutes)
            commit('SET_MENU', accessedRoutes)
            resolve(accessedRoutes)
          }else{
            const routes = []
            let accessedRoutes = filterAsyncRoutes(constantRoutes, routes)
            console.log('accessedRoutes------',accessedRoutes)
            commit('SET_MENU', accessedRoutes)
            router.addRoutes(accessedRoutes)
            resolve(accessedRoutes)
            // commit('SET_MENU', constantRoutes)
            // router.addRoutes(constantRoutes)
            // resolve(constantRoutes)
          }
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
